﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace HelperPrototype.OtherForms
{
    public partial class ThemNCCForm : Form
    {
        private string _maNCC = null;

        public ThemNCCForm()
        {
            InitializeComponent();
        }

        public ThemNCCForm(string MaNhaCungCap)
        {
            InitializeComponent();
            _maNCC = MaNhaCungCap;
            this.Text = "Chỉnh sửa thông tin nhà cung cấp";
            loadData();
        }

        private void loadData()
        {
            _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
            _DB.NhaCungCap s = (from NhaCungCap in dc.NhaCungCaps
                                where NhaCungCap.MaNhaCungCap == _maNCC
                                select NhaCungCap).Single();

            txtMaNCC.ReadOnly = true;
            txtMaNCC.Text = s.MaNhaCungCap;
            txtName.Text = s.TenNhaCungCap;
            txtTradename.Text = s.TenGiaoDich;
            txtTaxCode.Text = s.MaThue;
            txtBankAccount.Text = s.TaiKhoanNganHang;
            txtAddress.Text = s.DiaChi;
            txtPhoneNumber.Text = s.DienThoai;
            txtFaxNumber.Text = s.SoFax;
            txtContractor.Text = s.NguoiDaiDien;
            txtEmail.Text = s.Email;
            txtNote.Text = s.GhiChu;
            txtStatus.Text = s.TrangThai;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (txtName.Text.Equals(""))
            {
                MessageBox.Show("Hãy điền tên nhà cung cấp!");
                return;
            }

            try
            {
                _DB.DataClasses1DataContext dc = new _DB.DataClasses1DataContext();
                _DB.NhaCungCap s;

                if (_maNCC == null)
                {
                    s = new _DB.NhaCungCap();
                    s.MaNhaCungCap = txtMaNCC.Text;
                }
                else
                    s = (from NhaCungCap in dc.NhaCungCaps
                         where NhaCungCap.MaNhaCungCap == _maNCC
                         select NhaCungCap).Single();

                s.TenNhaCungCap = txtName.Text;
                s.TenGiaoDich = txtTradename.Text;
                s.MaThue = txtTaxCode.Text;
                s.TaiKhoanNganHang = txtBankAccount.Text;
                s.DiaChi= txtAddress.Text;
                s.DienThoai = txtPhoneNumber.Text;
                s.SoFax = txtFaxNumber.Text;
                s.NguoiDaiDien = txtContractor.Text;
                s.Email = txtEmail.Text;
                s.GhiChu = txtNote.Text;
                s.TrangThai = txtStatus.Text;

                if (_maNCC == null)
                    dc.NhaCungCaps.InsertOnSubmit(s);

                dc.SubmitChanges();

                this.Close();
            }
            catch (System.Exception ex)
            {
            	MessageBox.Show(ex.Message, "Error");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
